import scrapy
import re
import datetime
from scrapy.loader.processors import MapCompose, TakeFirst


def detail_content(content_list):
    content = ''
    for i in content_list:
        content += i + '\n'
    return content


def detail_datetime(date_time):
    # 获取当前时间
    now = datetime.datetime.now()

    # 将传入的时间统一处理为2021-1-1 12：20：10格式
    m = re.search('分钟前', date_time)
    h = re.search('小时前', date_time)
    sz = int(re.search('\d+', date_time).group(0))
    if m:
        time = (now + datetime.timedelta(minutes=-sz)).strftime("%Y-%m-%d %H:%M:%S")
    elif h:
        time = (now + datetime.timedelta(hours=-sz)).strftime("%Y-%m-%d %H:%M:%S")
    else:
        time = (re.sub(r'年|月', '-', date_time)).replace('日', "") + ":00"
    time = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S")
    return time


class BaiduItem(scrapy.Item):
    # define the fields for your item here like:
    title = scrapy.Field(
        output_processor=TakeFirst()
    )
    content = scrapy.Field(
        input_processor=MapCompose(detail_content),
        output_processor=TakeFirst()
    )
    datetime = scrapy.Field(
        input_processor=MapCompose(detail_datetime),
        output_processor=TakeFirst()
    )
    source = scrapy.Field(
        output_processor=TakeFirst()
    )
    keyword = scrapy.Field(
        output_processor=TakeFirst()
    )
    x_href = scrapy.Field(
        output_processor=TakeFirst()
    )


class QCCItem(scrapy.Item):
    reoresent = scrapy.Field()  # 法定代表人
    set_date = scrapy.Field()   # 成立日期
    import_man = scrapy.Field()  # 主要人员
    shar_man = scrapy.Field()  # 股东信息
    tel = scrapy.Field()  # 联系电话
    reg_ca = scrapy.Field()  # 注册资本
    com_num = scrapy.Field()  # 企业人数
    last_name = scrapy.Field()  # 曾用名






